<!DOCTYPE html>
<HTML>
<HEAD><meta name="viewport" content="width=device-width, initial-scale=1">
<Title>The First JSP Project</Title>
<link type='text/css' rel='Stylesheet' href="maxchartapi.css" />
</HEAD>
<body bgcolor="#FFFFFF" text="#000000" topmargin="0" leftmargin="0" rightmargin="0" marginwidth="0" marginheight="0">
<p class="heading0">ChartDirector 7.0 (Java Edition)</p>
<p class="heading1">The First JSP Project</p>
<hr class="separator">
<div class="content"><img src="images/simplebar.png" width='250' height='250'><br><br>
To get a feeling of using ChartDirector, and to verify the ChartDirector development environment is set up properly, we will begin by building a very simple bar chart.<br><br>
The following code is based on the sample programs that come with ChartDirector. If you have not yet installed the sample programs, it is highly recommended you install them now. Please refer to the <a href="install.htm"> Installation</a> section for details. They are very useful for exploring and testing the features of ChartDirector.<br><br>
<b>[JSP Version]</b> jspdemo/simplebar.jsp
<div class='codeblock'><code class='pre'>&lt;%@page import="ChartDirector.*, java.util.*" %&gt;
&lt;%
// The data for the bar chart
double[] data = {85, 156, 179.5, 211, 123};

// The labels for the bar chart
String[] labels = {"Mon", "Tue", "Wed", "Thu", "Fri"};

// Create a XYChart object of size 250 x 250 pixels
XYChart c = new XYChart(250, 250);

// Set the plotarea at (30, 20) and of size 200 x 200 pixels
c.setPlotArea(30, 20, 200, 200);

// Add a bar chart layer using the given data
c.addBarLayer(data);

// Set the labels on the x axis.
c.xAxis().setLabels(labels);

// Output the chart
WebChartViewer viewer = new WebChartViewer(request, "chart1");
viewer.setChart(c, Chart.SVG);

// Include tool tip for the chart
viewer.setImageMap(c.getHTMLImageMap("", "", "title='{xLabel}: {value} GBytes'"));
%&gt;
&lt;!DOCTYPE html&gt;
&lt;html&gt;
&lt;head&gt;
    &lt;title&gt;Simple Bar Chart (1)&lt;/title&gt;
    &lt;!-- Include ChartDirector Javascript Library to support chart interactions --&gt;
    &lt;script type="text/javascript" src="cdjcv.js"&gt;&lt;/script&gt;
&lt;/head&gt;
&lt;body style="margin:5px 0px 0px 5px"&gt;
    &lt;div style="font:bold 18pt verdana;"&gt;
        Simple Bar Chart (1)
    &lt;/div&gt;
    &lt;hr style="border:solid 1px #000080; background:#000080" /&gt;
    &lt;div style="font:10pt verdana; margin-bottom:1.5em"&gt;
        &lt;a href="viewsource.jsp?file=&lt;%=request.getServletPath()%&gt;"&gt;View Source Code&lt;/a&gt;
    &lt;/div&gt;
    &lt;!-- ****** Here is the chart image ****** --&gt;
    &lt;%= viewer.renderHTML(response) %&gt;
&lt;/body&gt;
&lt;/html&gt;</code></div><br>
The code is explained below:<br><br>
<ul>
<li><code>&lt;%@page import=<wbr>"ChartDirector.*, java.util.*" %&gt;</code><br><br>
The above line imports the ChartDirector namespace into the program to simplify the code. Instead of writing "ChartDirector.XYChart", you can simply write "XYChart".<br><br>
<li><code>XYChart c = new <a href="XYChart.XYChart.htm">XYChart</a>(<wbr>250, 250);</code><br><br>
The first step in creating any chart in ChartDirector is to create the appropriate chart object. In this example, an <a href="XYChart.htm">XYChart</a> object of size 250 x 250 pixels is created. In ChartDirector, XYChart represents any chart that has x- axis and y-axis, such as the bar chart we are drawing.<br><br>
<li><code>c.<a href="XYChart.setPlotArea.htm">setPlotArea</a>(<wbr>30, 20, 200, 200);</code><br><br>
The second step in creating a bar chart is to specify where should we draw the bar chart. This is by specifying the rectangle that contains the bar chart. The rectangle is specified by using the (x, y) coordinates of its top-left corner, together with its width and height.<br><br>
For this simple bar chart, we will use the majority of the chart area to draw the bar chart. We will leave some margin to allow for the text labels on the axis. In the above code, the top-left corner is set to (30, 30), and both the width and height is set to 200 pixels. Since the entire chart is 250 x 250 in size, there will be 20 to 30 pixels margin for the text labels.<br><br>
Note that ChartDirector uses a pixel coordinate system that is customary for computer screen. The x pixel coordinate is increasing from left to right. The y pixel coordinate is increasing from top to bottom. The origin (0, 0) is at the top-left corner.<br><br>
For more complex charts which may contain titles, legend box and other things, we can use this method (and other methods) to design the exact layout of the entire chart.<br><br>
<li><code>c.<a href="XYChart.addBarLayer.htm">addBarLayer</a>(<wbr>data);</code><br><br>
The above code adds a bar layer to the XYChart. In ChartDirector, any chart type that has x-axis and y-axis is represented as a layer in the XYChart. An XYChart can contain multiple layers. This allows "combination charts" to be created easily by combining different layers on the same chart (eg., a chart containing a line layer on top of a bar layer) .<br><br>
In the above line of code, the argument is an array of numbers representing the values of the data points.<br><br>
<li><code>c.<a href="XYChart.xAxis.htm">xAxis</a>(<wbr>).<a href="Axis.setLabels.htm">setLabels</a>(<wbr>labels);</code><br><br>
The above code sets the labels on the x-axis. The first method <a href="XYChart.xAxis.htm">XYChart.xAxis</a> retrieves the <a href="Axis.htm">Axis</a> object that represents the x-axis. The second method <a href="Axis.setLabels.htm">Axis.setLabels</a> binds the text labels to the x-axis. The argument to the setLabels method is an array of text strings.<br><br>
<li><code>WebChartViewer viewer = new <a href="WebChartViewer.WebChartViewer.htm">WebChartViewer</a>(<wbr>request, "chart1");<br>viewer.<a href="WebChartViewer.setChart.htm">setChart</a>(<wbr>c, <a href="Chart.SVG.htm">Chart.SVG</a>);</code><br><br>
Up to this point, the chart is completed. In this example, the chart is passed to a <a href="WebChartViewer.htm">WebChartViewer</a> control to be output in SVG format.<br><br>
<li><code>String imageMap1 = c.<a href="BaseChart.getHTMLImageMap.htm">getHTMLImageMap</a>(<wbr>"", "", "title=<wbr>'{xLabel}: US${value}K'");</code><br><br>
After displaying the chart, an image map can be created for the chart to configure mouse interactions. In this example, the image map is configured to display tool tips of the format <code>"title='{xLabel}: US${value}K'"</code> when the mouse cursor is on top of the bars.<br><br>
Note that mouse interactions are optional. Your can always leave out the <a href="BaseChart.getHTMLImageMap.htm">BaseChart.getHTMLImageMap</a> code if mouse interactions are not required.<br><br>
<li><code>&lt;script type=<wbr>"text/<wbr>javascript" src=<wbr>"cdjcv.js"&gt;&lt;/<wbr>script&gt;</code><br><br>
In this example, we include the <a href='javascriptlib.htm'>ChartDirector Javascript Library</a> "cdjcv.js" in the web page. This library provides support for various advanced ChartDirector features, such as responsive design, CDML tooltips, real-time update, programmable track cursor and zooming and scrolling. Although it is not needed for this simple example, we suggest to include "cdjcv.js" in all web pages that use ChartDirector.<br><br>
<li><code>&lt;%= viewer.<a href="WebChartViewer.renderHTML.htm">renderHTML</a>(<wbr>response) %&gt;</code><br><br>
The above line generates the HTML tags representing the chart and inserts them into the web page.<br></ul>
<div class="greyblock"> <b>Note:</b> The trial version of ChartDirector will include small yellow banners at the bottom of the charts it produces. These banners will disappear in the licensed version of ChartDirector. </div></div><br>
<hr class="separator"><div class="copyright">&copy; 2022 Advanced Software Engineering Limited. All rights reserved.</div></body>
</HTML>
